from django.db import models

# Create your models here.


class BaseModel(models.Model):
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    update_time = models.DateTimeField(auto_now=True, null=True)

    class Meta:
        abstract = True


class UserProfile(BaseModel, models.Model):
    name = models.CharField(max_length=30, unique=True)  # 姓名
    avatar = models.ImageField(null=True, upload_to='avatars')  # 头像

    class Meta:
        db_table = 'ihome_user_profile'


class UserAuth(BaseModel, models.Model):
    id_name = models.CharField(max_length=10)  # 实名认证的姓名
    id_card = models.CharField(max_length=18, unique=True)  # 实名认证的身份证号码

    class Meta:
        db_table = 'ihome_user_auth'


class User(BaseModel, models.Model):
    phone = models.CharField(max_length=11, unique=True)  # 手机号
    secret_pwd = models.CharField(max_length=200)  # 密码
    user_profile = models.OneToOneField(UserProfile, on_delete=models.SET_NULL, null=True)
    user_auth = models.OneToOneField(UserAuth, on_delete=models.CASCADE, null=True)

    class Meta:
        db_table = 'ihome_user'


class Facility(BaseModel, models.Model):
    name = models.CharField(max_length=32, null=False)  # 设施名字
    css = models.CharField(max_length=30, null=True)  # 设施展示的图标

    class Meta:
        db_table = 'ihome_facility'


class Area(BaseModel, models.Model):
    name = models.CharField(max_length=32, null=False)  # 区域名字

    class Meta:
        db_table = 'ihome_area'


class House(BaseModel, models.Model):
    title = models.CharField(max_length=64, null=False)  # 标题
    price = models.IntegerField(default=0)  # 单价，单位：分？
    address = models.CharField(max_length=512, default='')  # 地址
    room_count = models.IntegerField(default=1)  # 房间数目
    acreage = models.IntegerField(default=0)  # 房间面积
    unit = models.CharField(max_length=32, default='')  # 房屋户型
    capacity = models.IntegerField(default=1)  # 房屋容纳的人数
    beds = models.CharField(max_length=64, default='')  # 房屋床铺的配置
    deposit = models.IntegerField(default=0)  # 房屋押金
    min_days = models.IntegerField(default=1)  # 最少入住天数
    max_days = models.IntegerField(default=0)  # 最多入住天数，0表示不限制
    order_count = models.IntegerField(default=0)  # 预定完成的该房屋的订单数
    index_image_url = models.CharField(max_length=256, default='')  # 房屋主图片的路劲
    # 多对一关联用户
    user_h = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    # 多对一关联所在区域
    area_h = models.ForeignKey(Area, on_delete=models.CASCADE, null=True)
    # 多对多关联设施
    facility = models.ManyToManyField(Facility, null=True)

    class Meta:
        db_table = 'ihome_house'


